<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>express-base | H5教案</title>

  
  <meta name="author" content="John Doe">
  

  
  <meta name="description" content="Express基础基于 Node.js 平台，快速、开放、极简的 web 开发框架。
123npm install express --save或者yarn add express -S
一、特色1、Web 应用Express 是一个基于 Node.js 平台的极简、灵活的 web 应用开发框架，它">
  

  
  
  <meta name="keywords" content="">
  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <meta property="og:title" content="express-base"/>

  <meta property="og:site_name" content="H5教案"/>

  
  <meta property="og:image" content="/ericxie-book/favicon.ico"/>
  

  <link href="/ericxie-book/favicon.ico" rel="icon">
  <link rel="alternate" href="/ericxie-book/atom.xml" title="H5教案" type="application/atom+xml">
  <link rel="stylesheet" href="/ericxie-book/css/style.css" media="screen" type="text/css">
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/ericxie-book/">H5教案</a>
    </h1>
    <p class="site-description"></p>
  </div>
  <nav class="site-navigation">
    <ul>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    <article>

  
    
    <h3 class="article-title"><span>express-base</span></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/27/express-base/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-27T07:27:47.000Z">
          2021-01-27
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="Express基础"><a href="#Express基础" class="headerlink" title="Express基础"></a>Express基础</h2><p>基于 Node.js 平台，快速、开放、极简的 web 开发框架。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">npm install express --save</span><br><span class="line">或者</span><br><span class="line">yarn add express -S</span><br></pre></td></tr></table></figure>
<h3 id="一、特色"><a href="#一、特色" class="headerlink" title="一、特色"></a>一、特色</h3><h4 id="1、Web-应用"><a href="#1、Web-应用" class="headerlink" title="1、Web 应用"></a>1、Web 应用</h4><p>Express 是一个基于 Node.js 平台的极简、灵活的 web 应用开发框架，它提供一系列强大的特性，帮助你创建各种 Web 和移动设备应用。</p>
<h4 id="2、API"><a href="#2、API" class="headerlink" title="2、API"></a>2、API</h4><p>丰富的 HTTP 快捷方法和任意排列组合的 Connect 中间件，让你创建健壮、友好的 API 变得既快速又简单。</p>
<h4 id="3、性能"><a href="#3、性能" class="headerlink" title="3、性能"></a>3、性能</h4><p>Express 不对 Node.js 已有的特性进行二次抽象，我们只是在它之上扩展了 Web 应用所需的基本功能。</p>
<h3 id="二、安装"><a href="#二、安装" class="headerlink" title="二、安装"></a>二、安装</h3><p>首先假定你已经安装了 Node.js，接下来为你的应用创建一个目录，然后进入此目录并将其作为当前工作目录。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">mkdir myapp</span><br><span class="line"><span class="built_in">cd</span> myapp</span><br></pre></td></tr></table></figure>
<p>通过 npm init 命令为你的应用创建一个 package.json 文件。 欲了解 package.json 是如何起作用的，请参考 Specifics of npm’s package.json handling。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm init</span><br></pre></td></tr></table></figure>
<p>此命令将要求你输入几个参数，例如此应用的名称和版本。 你可以直接按“回车”键接受默认设置即可，下面这个除外：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">entry point: (index.js)</span><br></pre></td></tr></table></figure>
<p>键入 app.js 或者你所希望的名称，这是当前应用的入口文件。如果你希望采用默认的 index.js 文件名，只需按“回车”键即可。</p>
<p>接下来安装 Express 并将其保存到依赖列表中：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install express --save</span><br></pre></td></tr></table></figure>
<p>如果只是临时安装 Express，不想将它添加到依赖列表中，只需略去 –save 参数即可：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install express</span><br></pre></td></tr></table></figure>
<p>安装 Node 模块时，如果指定了 –save 参数，那么此模块将被添加到 package.json 文件中 dependencies 依赖列表中。 然后通过 npm install 命令即可自动安装依赖列表中所列出的所有模块。</p>
<h3 id="三、Hello-world-实例"><a href="#三、Hello-world-实例" class="headerlink" title="三、Hello world 实例"></a>三、Hello world 实例</h3><p>接下来，我们一起创建一个基本的 Express 应用。</p>
<p>注意：这里所创建是一个最最简单的 Express 应用，并且仅仅只有一个文件 — 和通过 Express 应用生成器 所创建的应用完全不一样，Express 应用生成器所创建的应用框架包含多 JavaScript 文件、Jade 模板和针对不同用途的子目录。</p>
<p>进入 myapp 目录，创建一个名为 app.js 的文件，然后将下列代码复制进去：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">var express = require(<span class="string">&#x27;express&#x27;</span>);</span><br><span class="line">var app = express();</span><br><span class="line"></span><br><span class="line">app.get(<span class="string">&#x27;/&#x27;</span>, <span class="keyword">function</span> (req, res) &#123;</span><br><span class="line">  res.send(<span class="string">&#x27;Hello World!&#x27;</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">var server = app.listen(3000, <span class="function"><span class="title">function</span></span> () &#123;</span><br><span class="line">  var host = server.address().address;</span><br><span class="line">  var port = server.address().port;</span><br><span class="line"></span><br><span class="line">  console.log(<span class="string">&#x27;Example app listening at http://%s:%s&#x27;</span>, host, port);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>上面的代码启动一个服务并监听从 3000 端口进入的所有连接请求。他将对所有 (/) URL 或 路由 返回 “Hello World!” 字符串。对于其他所有路径全部返回 404 Not Found。</p>
<p>req (请求) 和 res (响应) 与 Node 提供的对象完全一致，因此，你可以调用 req.pipe()、req.on(‘data’, callback) 以及任何 Node 提供的方法。</p>
<p>通过如下命令启动此应用：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">node app.js</span><br></pre></td></tr></table></figure>
<p>然后在浏览器中打开 [<a target="_blank" rel="noopener" href="http://localhost:3000/]">http://localhost:3000/]</a> 并查看输出结果</p>
<h3 id="四、express的通信应用"><a href="#四、express的通信应用" class="headerlink" title="四、express的通信应用"></a>四、express的通信应用</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">const express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">const app=express();</span><br></pre></td></tr></table></figure>
<h4 id="app-get"><a href="#app-get" class="headerlink" title="app.get"></a>app.get</h4><p>主要用于前后端get通信方式处理</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line"></span><br><span class="line">app.get(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">    res.send(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h4 id="app-post"><a href="#app-post" class="headerlink" title="app.post"></a>app.post</h4><p>主要用于前后端post通信方式处理</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line"></span><br><span class="line">app.post(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">    res.send(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h4 id="app-put-app-delete-app-all"><a href="#app-put-app-delete-app-all" class="headerlink" title="app.put,app.delete,app.all"></a>app.put,app.delete,app.all</h4><p>1、GET请求会向数据库发索取数据的请求，从而来获取信息，该请求就像数据库的select操作一样，只是用来查询一下数据，不会修改、增加数据，不会影响资源的内容，即该请求不会产生副作用。无论进行多少次操作，结果都是一样的。</p>
<p>2、与GET不同的是，PUT请求是向服务器端发送数据的，从而改变信息，该请求就像数据库的update操作一样，用来修改数据的内容，但是不会增加数据的种类等，也就是说无论进行多少次PUT操作，其结果并没有不同。</p>
<p>3、POST请求同PUT请求类似，都是向服务器端发送数据的，但是该请求会改变数据的种类等资源，就像数据库的insert操作一样，会创建新的内容。几乎目前所有的提交操作都是用POST请求的。</p>
<p>4、DELETE请求顾名思义，就是用来删除某一个资源的，该请求就像数据库的delete操作。</p>
<p>app.all是不管哪种都可以接收</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line"></span><br><span class="line">//app.put(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">//    res.send(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line">//&#125;)</span><br><span class="line">//app.delete(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">//    res.send(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line">//&#125;)</span><br><span class="line">//app.all(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">//    res.send(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line">//&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h4 id="app-use"><a href="#app-use" class="headerlink" title="app.use"></a>app.use</h4><p>use一般用于中间件，目的是在发送之前，或者某件事件之前先做一些处理，例如提前处理一些数据<br>后面我们会详细讲解各种中间价的处理</p>
<p>注意use后面的第二个函数中有3个参数，第一个是req，第二个是res，第三个是next<br>我们在use中处理完内容后，必须执行next()这样才可以进入后续的内容，否则将不会向后继续执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line">app.use(<span class="string">&quot;/&quot;</span>,<span class="keyword">function</span>(req,res,next)&#123;</span><br><span class="line">    res.set(&#123;<span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>:<span class="string">&#x27;*&#x27;</span>&#125;);//例如在这里处理跨域问题</span><br><span class="line">    next();</span><br><span class="line">&#125;)</span><br><span class="line">app.get(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">    console.log(<span class="string">&quot;bb&quot;</span>);</span><br><span class="line">    res.send(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h4 id="app-engine"><a href="#app-engine" class="headerlink" title="app.engine"></a>app.engine</h4><p>注册扩展名解析，如果遇到某种扩展名，使用某个方法解析该扩展名的文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">app.engine(<span class="string">&quot;art&quot;</span>,require(<span class="string">&quot;express-art-template&quot;</span>));</span><br><span class="line">//遇到art扩展名文件，使用express-art-template插件解析</span><br></pre></td></tr></table></figure>
<h4 id="app-set"><a href="#app-set" class="headerlink" title="app.set"></a>app.set</h4><p>部分第三方的一些插件在express框架中使用时需要设置一些基础参数，便于在使用时直接调用，这里有一部分是固定属性，也可以自定义属性</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">//设置views文件夹也就是模板文件夹，对于调用的路径是view文件夹下</span><br><span class="line">app.set(<span class="string">&quot;views&quot;</span>,path.join(__dirname,<span class="string">&quot;./view&quot;</span>));</span><br><span class="line">//设置视图解析的方式是通过art这个注册的插件</span><br><span class="line">app.set(<span class="string">&quot;view engine&quot;</span>,<span class="string">&quot;art&quot;</span>);</span><br></pre></td></tr></table></figure>
<h3 id="五、express中请求的部分属性"><a href="#五、express中请求的部分属性" class="headerlink" title="五、express中请求的部分属性"></a>五、express中请求的部分属性</h3><p>主要使用在各种get，post，use等app应用的第二个函数中的req的属性</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line">app.post(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">   //req 这里的req就是请求数据的内容</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h4 id="req-query"><a href="#req-query" class="headerlink" title="req.query"></a>req.query</h4><p>获取get通过地址传参方式传过来的参数</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line">app.use(<span class="string">&quot;/&quot;</span>,<span class="keyword">function</span>(req,res,next)&#123;</span><br><span class="line">    res.set(&#123;<span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>:<span class="string">&#x27;*&#x27;</span>&#125;);//例如在这里处理跨域问题</span><br><span class="line">    next();</span><br><span class="line">&#125;)</span><br><span class="line">app.get(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">    console.log(req.query);</span><br><span class="line">    res.send(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h4 id="req-body"><a href="#req-body" class="headerlink" title="req.body"></a>req.body</h4><p>获取post通过地址传参方式传过来的参数,但是node并不会自动解析传过来的数据，所以需要一个额外的插件用来解析，这个插件普通我们通常使用body-parser解析</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i body-parser -S</span><br></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line">const bodyParser=require(<span class="string">&quot;body-parser&quot;</span>);</span><br><span class="line"></span><br><span class="line">app.use(<span class="string">&quot;/&quot;</span>,<span class="keyword">function</span>(req,res,next)&#123;</span><br><span class="line">    res.set(&#123;<span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>:<span class="string">&#x27;*&#x27;</span>&#125;);</span><br><span class="line">    next();</span><br><span class="line">&#125;)</span><br><span class="line">// parse application/x-www-form-urlencode</span><br><span class="line">app.use(bodyParser.urlencoded(&#123;extended:<span class="literal">false</span>&#125;));</span><br><span class="line">// parse application/json</span><br><span class="line">app.use(bodyParser.json());</span><br><span class="line"></span><br><span class="line">app.post(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">    console.log(req.body);</span><br><span class="line">    res.send(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<p>例如解析formdata数据用到的插件multer</p>
<p>下载</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i multer -S</span><br></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line">const bodyParser=require(<span class="string">&quot;body-parser&quot;</span>);</span><br><span class="line">const multer=require(<span class="string">&quot;multer&quot;</span>)();//载入并且执行函数返回对象</span><br><span class="line"></span><br><span class="line">app.use(<span class="string">&quot;/&quot;</span>,<span class="keyword">function</span>(req,res,next)&#123;</span><br><span class="line">    res.set(&#123;<span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>:<span class="string">&#x27;*&#x27;</span>&#125;);</span><br><span class="line">    next();</span><br><span class="line">&#125;)</span><br><span class="line">// parse application/x-www-form-urlencode</span><br><span class="line">app.use(bodyParser.urlencoded(&#123;extended:<span class="literal">false</span>&#125;));</span><br><span class="line">// parse application/json</span><br><span class="line">app.use(bodyParser.json());</span><br><span class="line">// parse application/formdata</span><br><span class="line">app.use(multer.none());</span><br><span class="line">app.post(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">    console.log(req.body);</span><br><span class="line">    res.send(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h3 id="req-path"><a href="#req-path" class="headerlink" title="req.path"></a>req.path</h3><p>获取当前路径</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line">app.use(<span class="string">&quot;/&quot;</span>,<span class="keyword">function</span>(req,res,next)&#123;</span><br><span class="line">    res.set(&#123;<span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>:<span class="string">&#x27;*&#x27;</span>&#125;);//例如在这里处理跨域问题</span><br><span class="line">    next();</span><br><span class="line">&#125;)</span><br><span class="line">app.get(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">    console.log(req.path);//path 是/a</span><br><span class="line">    res.send(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h3 id="六、express中响应的部分属性"><a href="#六、express中响应的部分属性" class="headerlink" title="六、express中响应的部分属性"></a>六、express中响应的部分属性</h3><p>主要使用在各种get，post，use等app应用的第二个函数中的res的属性</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line">app.post(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">   //res 这里的req就是请求数据的内容</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h4 id="res-send"><a href="#res-send" class="headerlink" title="res.send"></a>res.send</h4><p>用于发送数据给前端</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line"></span><br><span class="line">app.use(<span class="string">&quot;/&quot;</span>,<span class="keyword">function</span>(req,res,next)&#123;</span><br><span class="line">    res.set(&#123;</span><br><span class="line">        <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>:<span class="string">&#x27;*&#x27;</span>,</span><br><span class="line">    &#125;);</span><br><span class="line">    next();</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.get(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">    res.send(Buffer.from(<span class="string">&#x27;whoop&#x27;</span>))//可以发送Buffer</span><br><span class="line">    // res.send(&#123; some: <span class="string">&#x27;json&#x27;</span> &#125;)//可以直接发送Json</span><br><span class="line">    // res.send(<span class="string">&#x27;&lt;p&gt;some html&lt;/p&gt;&#x27;</span>)//可以发送字符</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h4 id="res-status"><a href="#res-status" class="headerlink" title="res.status"></a>res.status</h4><p>返回状态码并且发送</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line"></span><br><span class="line">app.use(<span class="string">&quot;/&quot;</span>,<span class="keyword">function</span>(req,res,next)&#123;</span><br><span class="line">    res.set(&#123;</span><br><span class="line">        <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>:<span class="string">&#x27;*&#x27;</span>,</span><br><span class="line">    &#125;);</span><br><span class="line">    next();</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.get(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">    res.status(404).send(<span class="string">&#x27;Sorry, we cannot find that!&#x27;</span>)//可以设置响应的状态的同事发送</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h4 id="res-set"><a href="#res-set" class="headerlink" title="res.set"></a>res.set</h4><p>设置响应头</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line"></span><br><span class="line">app.use(<span class="string">&quot;/&quot;</span>,<span class="keyword">function</span>(req,res,next)&#123;</span><br><span class="line">    res.set(&#123;</span><br><span class="line">        <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>:<span class="string">&#x27;*&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;Content-Type&#x27;</span>:<span class="string">&#x27;text/html;charset=utf-8&#x27;</span></span><br><span class="line">    &#125;);</span><br><span class="line">    next();</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.get(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">   res.send(<span class="string">&#x27;aaa&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h4 id="res-type"><a href="#res-type" class="headerlink" title="res.type"></a>res.type</h4><p>设置响应头消息的MIME类型</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line"></span><br><span class="line">app.use(<span class="string">&quot;/&quot;</span>,<span class="keyword">function</span>(req,res,next)&#123;</span><br><span class="line">    res.set(&#123;</span><br><span class="line">        <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>:<span class="string">&#x27;*&#x27;</span>,</span><br><span class="line">    &#125;);</span><br><span class="line">    res.type(<span class="string">&quot;json&quot;</span>);</span><br><span class="line">    next();</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.get(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">    res.send(&#123;a:1,b:2&#125;);</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h4 id="res-json"><a href="#res-json" class="headerlink" title="res.json"></a>res.json</h4><p>直接设置响应消息的MIME类型是json,并且将对象用json.stringfy转换直接发送</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line"></span><br><span class="line">app.use(<span class="string">&quot;/&quot;</span>,<span class="keyword">function</span>(req,res,next)&#123;</span><br><span class="line">    res.set(&#123;</span><br><span class="line">        <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>:<span class="string">&#x27;*&#x27;</span>,</span><br><span class="line">    &#125;);</span><br><span class="line">    next();</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.get(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">    res.json(&#123;a:1,b:2&#125;);</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
<h4 id="res-jsonp"><a href="#res-jsonp" class="headerlink" title="res.jsonp"></a>res.jsonp</h4><p>直接发送jsonp消息</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">var express=require(<span class="string">&quot;express&quot;</span>);</span><br><span class="line">var app=express();</span><br><span class="line"></span><br><span class="line">app.use(<span class="string">&quot;/&quot;</span>,<span class="keyword">function</span>(req,res,next)&#123;</span><br><span class="line">    res.set(&#123;</span><br><span class="line">        <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>:<span class="string">&#x27;*&#x27;</span>,</span><br><span class="line">    &#125;);</span><br><span class="line">    next();</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.get(<span class="string">&quot;/a&quot;</span>,<span class="keyword">function</span>(req,res)&#123;</span><br><span class="line">    res.jsonp(&#123;a:1,b:2&#125;);//返回的消息在callback函数中调用</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(4001);</span><br></pre></td></tr></table></figure>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>

  






    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2021 John Doe
    
  </p>
</footer>
    
    
  </div>
</div>
</body>
</html>